package com.moyu.article.controller;

import com.github.pagehelper.PageInfo;
import com.moyu.article.domain.comment.Comment;
import com.moyu.article.domain.comment.dto.CommentDTO;
import com.moyu.article.domain.comment.dto.CommentItemQueryDTO;
import com.moyu.article.domain.comment.dto.CommentQueryDTO;
import com.moyu.article.domain.comment.dto.DetailQueryDTO;
import com.moyu.article.domain.comment.vo.CommentListVO;
import com.moyu.article.domain.comment.vo.CommentQueryVO;
import com.moyu.article.service.ICommentService;
import com.moyu.common.core.controller.BaseController;
import com.moyu.common.core.domain.LoginUser;
import com.moyu.common.core.domain.R;
import com.moyu.common.core.domain.Result;
import com.moyu.common.core.domain.TableDataInfo;

import java.util.ArrayList;
import java.util.List;

import com.moyu.common.core.utils.JwtUtils;
import com.moyu.common.security.service.TokenService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 赵彦博
 * @Date 2025/3/30 9:27
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private TokenService tokenService;

    @Value("${jwt.secret}")
    private String secret;

    @GetMapping("/list")
    public TableDataInfo list(CommentQueryDTO query) {
        return getTableDataInfo(commentService.list(query));
    }

    // 修正2：使用@PathVariable正确获取路径参数;
    @PutMapping("/approve/{commentId}")
    public R approveComment(@PathVariable String commentId) {
        return toR(commentService.approveComment(commentId));
    }

    // 修正3：使用@PathVariable正确获取路径参数
    @PutMapping("/reject/{commentId}")
    public R rejectComment(@PathVariable String commentId) {
        return toR(commentService.rejectComment(commentId));
    }

    @GetMapping("/getCommentByPost")
    public List<String> getCommentByPost(DetailQueryDTO dto) {
        return commentService.getCommentByPost(dto);
    }

    /**
     * 获取评论列表
     */
    @GetMapping("/get")
    public List<CommentQueryVO> getComments(@RequestParam Long postId) {
        Long userId = tokenService.getLoginUserId();
        List<CommentQueryVO> comments = commentService.getComments(postId, userId);
        return comments;
    }

    /**
     * 创建评论
     */
    @PostMapping("/create")
    public CommentQueryVO createComment(@RequestBody CommentDTO commentDTO) {
        String token = commentDTO.getToken();
        LoginUser loginUser = tokenService.getLoginUser(token, secret);
        String nickName = loginUser.getNickName();
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return null;
        }
        commentDTO.setUserId(userId);
        commentDTO.setNickName(nickName);
        Comment comment1 = commentService.createComment(commentDTO);
        CommentQueryVO comment = new CommentQueryVO();
        comment.setCreateBy(String.valueOf(comment1.getCreateBy()));
        comment.setCommentId(String.valueOf(comment1.getCommentId()));
        String s = String.valueOf(commentDTO.getParentId());
        comment.setParentId(s);
        comment.setPostId(String.valueOf(commentDTO.getPostId()));
        comment.setContent(comment1.getContent());
        comment.setPostTitle(comment1.getPostTitle());
        comment.setIsLiked(comment1.getIsLiked());
        comment.setUserId(String.valueOf(comment1.getUserId()));
        comment.setNickName(comment1.getNickName());
        comment.setIsTop(comment1.getIsTop());
        comment.setCommentId(String.valueOf(comment1.getCommentId()));
        if (comment.getChildren() == null) {
            comment.setChildren(new ArrayList<>());
        }
        return comment;
    }

    /**
     * 点赞评论
     */
    @PostMapping("/{commentId}/like")
    public Boolean likeComment(@PathVariable Long commentId) {
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return false;
        }
        commentService.likeComment(commentId, userId);
        return true;
    }

    /**
     * 取消点赞评论
     */
    @DeleteMapping("/{commentId}/cancelLike")
    public Boolean cancelLikeComment(@PathVariable Long commentId) {
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return false;
        }
        commentService.cancelLikeComment(commentId, userId);
        return true;
    }
}
